package com.base.study.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.utils.GlideUtil;
import com.base.commonlib.utils.SubStringUtil;
import com.base.commonlib.utils.ToastUtil;
import com.base.study.R;
import com.base.study.bean.FromStudyTypeistBean;

import java.util.List;

/**
 * Created by zw on 2020/2/27.
 */

public class ForTyoeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final static int HEAD_COUNT = 1;
    private final static int FOOT_COUNT = 0;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_CONTENT = 1;
    private final static int TYPE_FOOTER = 2;
    private Context context;
    private List<FromStudyTypeistBean> list;
    private String headTitle;
    private boolean isShowHead = true;
    private String id;

    public ForTyoeAdapter(Context context, Activity activity, List<FromStudyTypeistBean> list, String headTitle,String id) {
        this.context = context;
        this.list = list;
        this.headTitle = headTitle;
        this.id = id;
    }
    public ForTyoeAdapter(Context context, Activity activity, List<FromStudyTypeistBean> list, String headTitle) {
        this.context = context;
        this.list = list;
        this.headTitle = headTitle;
    }

    public void setShowHead(boolean showHead) {
        isShowHead = showHead;
    }

    public int getContentSize() {
        return list.size();
    }

    public boolean isHead(int position) {
        return HEAD_COUNT != 0 && position == 0;
    }

    public boolean isFoot(int position) {
        return FOOT_COUNT != 0 && position == getContentSize() + HEAD_COUNT;
    }

    @Override
    public int getItemViewType(int position) {
        int contentSize = getContentSize();
        if (HEAD_COUNT != 0 && position == 0) { // 头部
            return TYPE_HEAD;
        } else if (FOOT_COUNT != 0 && position == HEAD_COUNT + contentSize) { // 尾部
            return TYPE_FOOTER;
        } else {
            return TYPE_CONTENT;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEAD) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_from_type_header, parent, false);
            return new ForTyoeAdapter.HeadHolder(itemView);
        } else if (viewType == TYPE_CONTENT) {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_from_type, parent, false);
            return new ForTyoeAdapter.ContentHolder(itemView);
        } else {
            View itemView = LayoutInflater.from(context).inflate(R.layout.item_from_type_header, parent, false);
            return new ForTyoeAdapter.FootHolder(itemView);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof ForTyoeAdapter.HeadHolder) { // 头部
            ForTyoeAdapter.HeadHolder headHolder = (ForTyoeAdapter.HeadHolder) holder;
            if (isShowHead) {
                headHolder.title.setVisibility(View.VISIBLE);
                headHolder.title.setText(headTitle);
            } else {
                headHolder.title.setVisibility(View.GONE);
            }

        } else if (holder instanceof ForTyoeAdapter.ContentHolder) { // 内容
            ForTyoeAdapter.ContentHolder myHolder = (ForTyoeAdapter.ContentHolder) holder;
            myHolder.title.setText(SubStringUtil.subStringCN(list.get(position - 1).getName(), 30));
            myHolder.des.setText(SubStringUtil.subStringCN(list.get(position - 1).getName(), 30));
            GlideUtil.load(context, list.get(position - 1).getCover(), ((ContentHolder) holder)
                    .item_image);
            myHolder.lin_item.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    //type 0、课程分类  1、新闻
//                    ARouter.getInstance().build("/curriculum/detail")
//                            .withString("id", list.get(position - 1).getId())
//                            .withString("url", list.get(position - 1).getCover())
//                            .navigation();


                    if (list.get(position-1).getType() == 0) {
                        ARouter.getInstance().build("/curriculum/detail")
                                .withString("id", list.get(position - 1).getId())
                                .withString("url", list.get(position - 1).getCover())
                                .withString("courseRootId",list.get(position-1).getFirstCategory())
                                .navigation();
                    } else if (list.get(position-1).getType() == 1) {
                        ARouter.getInstance().build("/home/homenew")
                                .withString("title", list.get(position - 1).getName())
                                .withString("courseRootId",list.get(position-1).getFirstCategory())
                          //      .withString("detail", list.get(position - 1).getDescription())
                                .withString("id", list.get(position - 1).getId())
                                .withString("type", id)//父id
                                .navigation();
                    }



                }
            });
        } else { // 尾部

        }
    }

    @Override
    public int getItemCount() {
        return list.size() + HEAD_COUNT + FOOT_COUNT;
    }

    // 头部
    private class HeadHolder extends RecyclerView.ViewHolder {
        TextView title;

        public HeadHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
        }
    }

    // 内容
    private class ContentHolder extends RecyclerView.ViewHolder {
        TextView title;
        TextView des;
        ImageView item_image;
        LinearLayout lin_item;

        public ContentHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.title);
            des = (TextView) itemView.findViewById(R.id.des);
            item_image = (ImageView) itemView.findViewById(R.id.item_image);
            lin_item = (LinearLayout) itemView.findViewById(R.id.lin_item);
        }
    }

    // 尾部
    private class FootHolder extends RecyclerView.ViewHolder {
        public FootHolder(View itemView) {
            super(itemView);
        }
    }
}
